home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / phpMyAdmin / libraries / bookmark.lib.php < prev    next >
PHP Script  |  2005-03-07  |  7KB  |  205 lines

  1. <?php
  2. /* $Id: bookmark.lib.php,v 2.12 2005/03/07 21:30:59 lem9 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Set of functions used with the bookmark feature
  7.  */
  8.  
  9.  
  10. /**
  11.  * Defines the bookmark parameters for the current user
  12.  *
  13.  * @return  array    the bookmark parameters for the current user
  14.  *
  15.  * @global  integer  the id of the current server
  16.  *
  17.  * @access  public
  18.  */
  19. function PMA_getBookmarksParam()
  20. {
  21.     global $server;
  22.  
  23.     $cfgBookmark = '';
  24.  
  25.     // No server selected -> no bookmark table
  26.     if ($server == 0) {
  27.         return '';
  28.     }
  29.  
  30.     $cfgBookmark['user']  = $GLOBALS['cfg']['Server']['user'];
  31.     $cfgBookmark['db']    = $GLOBALS['cfg']['Server']['pmadb'];
  32.     $cfgBookmark['table'] = $GLOBALS['cfg']['Server']['bookmarktable'];
  33.  
  34.     return $cfgBookmark;
  35. } // end of the 'PMA_getBookmarksParam()' function
  36.  
  37.  
  38. /**
  39.  * Gets the list of bookmarks defined for the current database
  40.  *
  41.  * @global  resource  the controluser db connection handle
  42.  *
  43.  * @param   string    the current database name
  44.  * @param   array     the bookmark parameters for the current user
  45.  *
  46.  * @return  mixed     the bookmarks list if defined, false else
  47.  *
  48.  * @access  public
  49.  */
  50. function PMA_listBookmarks($db, $cfgBookmark)
  51. {
  52.     global $dbh;
  53.  
  54.     $query  = 'SELECT label, id FROM '. PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
  55.             . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
  56.             . ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
  57.             . '      OR user = \'\')'
  58.             . ' ORDER BY label';
  59.     $result = PMA_DBI_query($query, $dbh, PMA_DBI_QUERY_STORE);
  60.  
  61.     // There are some bookmarks -> store them
  62.     // use the unique id as the key
  63.     if ($result > 0 && PMA_DBI_num_rows($result) > 0) {
  64.         while ($row = PMA_DBI_fetch_row($result)) {
  65.             $bookmark_list[$row[1]] = $row[0];
  66.         } // end while
  67.         return $bookmark_list;
  68.     }
  69.     // No bookmarks for the current database
  70.     else {
  71.         return FALSE;
  72.     }
  73. } // end of the 'PMA_listBookmarks()' function
  74.  
  75.  
  76. /**
  77.  * Gets the sql command from a bookmark
  78.  *
  79.  * @global  resource  the controluser db connection handle
  80.  *
  81.  * @param   string    the current database name
  82.  * @param   array     the bookmark parameters for the current user
  83.  * @param   mixed     the id of the bookmark to get
  84.  * @param   string    which field to look up the $id
  85.  * @param   boolean  TRUE: get all bookmarks regardless of the owning user
  86.  *
  87.  * @return  string    the sql query
  88.  *
  89.  * @access  public
  90.  */
  91. function PMA_queryBookmarks($db, $cfgBookmark, $id, $id_field = 'id', $action_bookmark_all = FALSE)
  92. {
  93.     global $dbh;
  94.  
  95.     if (empty($cfgBookmark['db']) || empty($cfgBookmark['table'])) {
  96.         return '';
  97.     }
  98.  
  99.     $query          = 'SELECT query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
  100.                     . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
  101.                     . ($action_bookmark_all? '' : ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
  102.                     . '      OR user = \'\')' )
  103.                     . ' AND ' . PMA_backquote($id_field) . ' = ' . $id;
  104.     $result = PMA_DBI_try_query($query, $dbh);
  105.     if (!$result) return FALSE;
  106.     list($bookmark_query) = PMA_DBI_fetch_row($result) or array(FALSE);
  107.  
  108.     return $bookmark_query;
  109. } // end of the 'PMA_queryBookmarks()' function
  110.  
  111.  
  112. /**
  113.  * Gets bookmarked DefaultQuery for a Table
  114.  *
  115.  * @global  resource  the controluser db connection handle
  116.  *
  117.  * @param   string    the current database name
  118.  * @param   array     the bookmark parameters for the current user
  119.  * @param   array     the list of all labels to look for
  120.  *
  121.  * @return  array     bookmark SQL statements
  122.  *
  123.  * @access  public
  124.  */
  125. function &PMA_queryDBBookmarks($db, $cfgBookmark, &$table_array)
  126. {
  127.     global $dbh;
  128.     $bookmarks = array();
  129.  
  130.     if (empty($cfgBookmark['db']) || empty($cfgBookmark['table'])) {
  131.         return $bookmarks;
  132.     }
  133.  
  134.     $search_for = array();
  135.     foreach($table_array AS $table => $table_sortkey) {
  136.         $search_for[] = "'" . PMA_sqlAddslashes($table) . "'";
  137.     }
  138.  
  139.     $query          = 'SELECT label, query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
  140.                     . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
  141.                     . (count($search_for) > 0 ? ' AND label IN (' . implode(', ', $search_for) . ')' : '');
  142.     $result = PMA_DBI_try_query($query, $dbh, PMA_DBI_QUERY_STORE);
  143.     if (!$result || PMA_DBI_num_rows($result) < 1) return $bookmarks;
  144.     while ($row = PMA_DBI_fetch_assoc($result)) {
  145.         $bookmarks[$row['label']] = $row['query'];
  146.     }
  147.  
  148.     return $bookmarks;
  149. } // end of the 'PMA_queryBookmarks()' function
  150.  
  151. /**
  152.  * Adds a bookmark
  153.  *
  154.  * @global  resource  the controluser db connection handle
  155.  *
  156.  * @param   array     the properties of the bookmark to add
  157.  * @param   array     the bookmark parameters for the current user
  158.  * @param   boolean   whether to make the bookmark available for all users
  159.  *
  160.  * @return  boolean   whether the INSERT succeeds or not
  161.  *
  162.  * @access  public
  163.  */
  164. function PMA_addBookmarks($fields, $cfgBookmark, $all_users = false)
  165. {
  166.     global $dbh;
  167.  
  168.     $query = 'INSERT INTO ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
  169.            . ' (id, dbase, user, query, label) VALUES (\'\', \'' . PMA_sqlAddslashes($fields['dbase']) . '\', \'' . ($all_users ? '' : PMA_sqlAddslashes($fields['user'])) . '\', \'' . PMA_sqlAddslashes(urldecode($fields['query'])) . '\', \'' . PMA_sqlAddslashes($fields['label']) . '\')';
  170.     $result   = PMA_DBI_query($query, $dbh);
  171.  
  172.     return TRUE;
  173. } // end of the 'PMA_addBookmarks()' function
  174.  
  175.  
  176. /**
  177.  * Deletes a bookmark
  178.  *
  179.  * @global  resource  the controluser db connection handle
  180.  *
  181.  * @param   string   the current database name
  182.  * @param   array    the bookmark parameters for the current user
  183.  * @param   integer  the id of the bookmark to get
  184.  *
  185.  * @access  public
  186.  */
  187. function PMA_deleteBookmarks($db, $cfgBookmark, $id)
  188. {
  189.     global $dbh;
  190.  
  191.     $query  = 'DELETE FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
  192.             . ' WHERE (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
  193.             . '        OR user = \'\')'
  194.             . ' AND id = ' . $id;
  195.     $result = PMA_DBI_try_query($query, $dbh);
  196. } // end of the 'PMA_deleteBookmarks()' function
  197.  
  198.  
  199. /**
  200.  * Bookmark Support
  201.  */
  202. $cfg['Bookmark'] = PMA_getBookmarksParam();
  203.  
  204. ?>
  205.